package com.tiancheng.trade.merchant.controller;

import com.tiancheng.trade.commom.core.utils.JsonUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.tiancheng.trade.merchant.util.UmsUtil.*;

/**
 * 跳转银联商户签约界面
 *
 * @author kellen
 * @date 2024-12-07 11:37
 */
@Slf4j
@Controller
@RequestMapping("/ums")
public class UmsMerchantController {
    // 调用地址
    private static final String URL = "https://yinshangpai.chinaums.com/self-sign-mobile/#/chooseRole";
    // 服务名称
    private static final String SERVICE = "merchant_reg";
    // 接入平台id
    private static final String ACCESSER_ID = "2d9081bd756ad12e01756de6c9892021";
    // 签名方式
    private static final String SIGN_TYPE = "SHA-256";
    // 签名密钥
    private static final String SIGN_KEY = "h4r0zqoy2lfi8dkl5qp5mdiy";

    private static final String ACCESSER_ACCT_TY_PREFIX = "腾云公司";
    private static final String ACCESSER_ACCT_KEY = "UMS_ACCESSER_ACCT_KEY";
    // 公司人员为商户进件时，支持3天内完善同一商户
    private static final Long ACCESSER_ACCT_DAY = 3L;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 内部专用
     *
     * @param request  {@link HttpServletRequest}
     * @param response {@link HttpServletResponse}
     */
    @GetMapping("/merchantRegister")
    public void merchantRegister(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 查询缓存
        String accesserAcctTy = stringRedisTemplate.opsForValue().get(ACCESSER_ACCT_KEY);

        if (StringUtils.isEmpty(accesserAcctTy)) {
            accesserAcctTy = ACCESSER_ACCT_TY_PREFIX + LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
            stringRedisTemplate.opsForValue().set(ACCESSER_ACCT_KEY, accesserAcctTy, ACCESSER_ACCT_DAY, TimeUnit.DAYS);
        }

        doRegister(request, response, accesserAcctTy);
    }

    /**
     * 外部专用
     *
     * @param request  {@link HttpServletRequest}
     * @param response {@link HttpServletResponse}
     */
    @GetMapping("/merchantRegisterOutside")
    public void merchantRegisterOutside(HttpServletRequest request, HttpServletResponse response) throws Exception {
        doRegister(request, response, acctHelper());
    }

    private void doRegister(HttpServletRequest request, HttpServletResponse response, String acct) throws Exception {
        // 1、原明文数据
        Map<String, Object> param = new HashMap<>();
        param.put("service", SERVICE);
        param.put("accesser_id", ACCESSER_ID);
        param.put("sign_type", SIGN_TYPE);
        param.put("request_date", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
        param.put("accesser_acct", acct);
        param.put("request_seq", requestSeqHelper());

        // 2、待签名的字符串
        String reqJson = JsonUtil.toJsonString(param);

        // 3、SHA-256签名
        String signedStr = sign(reqJson);

        // 4、3DES加密
        String desStr = encrypt(reqJson, SIGN_KEY);

        String umsUrl = URL + "?sign_data=" + signedStr + "&json_data=" + desStr + "&accesser_id=" + ACCESSER_ID;

        log.info("银联商户自助签约url: " + umsUrl);

        response.sendRedirect(umsUrl);
    }

}
